package cn.easier.activity.console.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.hibernate.validator.constraints.Length;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 后台用户信息
 */

@Entity(name = "activity_console_user")
@JsonIgnoreProperties(ignoreUnknown = true)
public class ActivityConsoleUser implements UserDetails {

    /**
     * 用户权限分隔符号
     */
    public transient static final String DEFAULT_SPLIT = ",";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long uid;

    @Column(columnDefinition = "VARCHAR(50) COMMENT '账号'", nullable = false, unique = true)
    private String username;

    @Column(columnDefinition = "VARCHAR(255) COMMENT '密码'")
    private String password;

    @Column(columnDefinition = "VARCHAR(30) COMMENT 'email'")
    private String email;

    @Column(columnDefinition = "VARCHAR(30) COMMENT '昵称'")
    private String nickName;

    @Column(columnDefinition = "bigint(100) COMMENT '手机号码'")
    private Long phoneNum;

    @Column(columnDefinition = "BIT(1) COMMENT '是否可用'", nullable = false)
    private Boolean enabled;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(columnDefinition = "DATETIME  COMMENT '注册日期'", updatable = false)
    private Date registerDate;

    @Column(columnDefinition = "VARCHAR(255) COMMENT '头像图片url'")
    private String headPortrait;

    @Column(columnDefinition = "VARCHAR(20) COMMENT '性别'")
    private String sex;

    /**
     * 如果此字段权限超过16个，可以直接在用户权限关联表获取权限组
     */
    @Column(columnDefinition = "VARCHAR(256) COMMENT '权限字符串(16个权限单位)'")
    private String authoritiesStr;

    @Transient
    private transient Set<GrantedAuthority> authorities = new HashSet<>(1);

    @Override
    public String toString() {
        return "ActivityConsoleUser{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                ", nickName='" + nickName + '\'' +
                ", phoneNum='" + phoneNum + '\'' +
                ", enabled=" + enabled +
                ", registerDate=" + registerDate +
                ", headPortrait='" + headPortrait + '\'' +
                ", sex='" + sex + '\'' +
                ", authoritiesStr='" + authoritiesStr + '\'' +
                ", authorities=" + authorities +
                '}';
    }

    public Long getUid() {
        return uid;
    }

    public void setUid(Long uid) {
        this.uid = uid;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public Date getRegisterDate() {
        return registerDate;
    }

    public void setRegisterDate(Date registerDate) {
        this.registerDate = registerDate;
    }

    public String getHeadPortrait() {
        return headPortrait;
    }

    public void setHeadPortrait(String headPortrait) {
        this.headPortrait = headPortrait;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAuthoritiesStr() {
        return authoritiesStr;
    }

    public void setAuthoritiesStr(String authoritiesStr) {
        this.authoritiesStr = authoritiesStr;
    }

    public Long getPhoneNum() {
        return phoneNum;
    }

    public void setPhoneNum(Long phoneNum) {
        this.phoneNum = phoneNum;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    public void setAuthorities(Set<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }
}
